﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;

public partial class PropertyList : System.Web.UI.UserControl
{
    private SPList imageList;
    private string propertyID="1";
    private DataTable dataTable;

    public DataTable ImageDataTable
    {
        get { return dataTable; }
        set { dataTable = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        propertyID = HttpContext.Current.Request.QueryString["id"];

        
        /* used in a test envrionment*/
      
        using (SPSite site = new SPSite("http://yu-pc:33333/sites/Home"))
        {
            using (SPWeb web = site.AllWebs[0])
            {
                string listName = "Property Pictures";
                imageList = web.Lists[listName];
                LoadImages(imageList);    
            }
        }
    
   
        /* used in sharepoint context */
        /*
        using (SPSite site = new SPSite(SPContext.Current.Site.ID))
        {
            using (SPWeb web = SPControl.GetContextSite(Context).OpenWeb())
            {
                string listName = "Property Pictures";
                imageList = web.Lists[listName];
                LoadImages(imageList); 
            }
        }
         * */
     

    }

    protected string getListPath() 
    {
        string listUrl = imageList.DefaultViewUrl;
        int index = listUrl.IndexOf("Forms");
        listUrl = listUrl.Remove(index);
        return listUrl;
    
    }

    private DataTable LoadImages(SPList list)
    {
        SPQuery qry = new SPQuery();

        if (!string.IsNullOrEmpty(propertyID))
        {
            qry.Query = "<Where><Eq><FieldRef Name='Property' LookupId='true'  /><Value Type='Lookup'>" + propertyID + "</Value></Eq></Where>"; 
        }

        

        dataTable = list.GetItems(qry).GetDataTable();

        if (dataTable == null) return null;

        foreach (DataRow dr in dataTable.Rows)
        {
            dr["LinkFilename"] = getListPath()+dr["LinkFilename"];
            this.ScriptLiteral.Text += "<li><a href=\"Lists/PropertyPictureLibrary/Forms/DispForm.aspx?ID=" + dr["ID"] + "\" ><img src=\"" + dr["LinkFilename"] + "\" alt=\"" + dr["NameOrTitle"] + "\" /><img src=\"" + dr["LinkFilename"] + "\" alt=\"" + dr["NameOrTitle"] + "\" class=\"preview\" /></a></li>";			
        }

        return dataTable;
    }
    
}
